import BaseTable from '../../TableTemplate/TableTemplate.vue';
export default {
    name: 'BudgetSupplement',
    props: ['data'],
    components: {
        BaseTable
    },
    data() {
        return {
            formModel: {
                invoiceTaxRate: '',
                projectBudget: '',
                amountExcludingTax: ''
            },
            formList: [
                {
                    label: '税率',
                    prop: 'invoiceTaxRate',
                    type: 'select',
                    options: [
                        { label: '0', value: '1' },
                        { label: '1', value: '2' },
                        { label: '3', value: '3' },
                        { label: '6', value: '4' },
                        { label: '9', value: '5' }
                    ],
                    unit: '%',
                    block: true
                },
                { label: '工程总造价', prop: 'projectBudget' },
                {
                    label: '不含税金额',
                    prop: 'amountExcludingTax'
                }
            ],
            tableConfig: [
                {
                    prop: 'projectNumber',
                    label: '项目编号',
                    width: '120px',
                    type: 'text',
                    text: '系统自动生成：B+8位流水号'
                },
                { prop: 'projectName', label: '项目名称', type: 'input', width: '100', showTips: true },
                { prop: 'featureDescription', label: '特征描述', type: 'input', width: '100', showTips: true },
                { prop: 'unit', label: '单位', type: 'input', width: '100' },
                {
                    prop: 'covertItem',
                    label: '是否隐藏项',
                    width: '100',
                    type: 'select',
                    options: [
                        {
                            label: '是',
                            value: '1'
                        },
                        {
                            label: '否',
                            value: '0'
                        }
                    ]
                },
                {
                    prop: 'quantities',
                    label: '工程量',
                    type: 'input',
                    width: '100'
                },
                {
                    prop: 'integratedUnitPrice',
                    label: '综合单价',
                    type: 'input',
                    width: '100'
                },
                {
                    prop: 'totalPrice',
                    label: '综合总价',
                    disabled: true,
                    type: 'input',
                    width: '100'
                },
                {
                    prop: 'materialsBrand',
                    label: '主材品牌',
                    type: 'input',
                    width: '100',
                    showTips: true
                },
                {
                    prop: 'remarks',
                    label: '备注',
                    type: 'input',
                    width: '100'
                },
                {
                    prop: 'approvalOpinion',
                    label: '审核意见',
                    width: '100'
                }
            ]
        };
    },
    mounted() {
        // 初始哈单价和综合量变化时的回调
        this.initQuantityReact();
        // 初始化税率变化时的回调
        this.initTaxChangeReact();
    },
    methods: {
        // 为工程量、综合单价输入框注册input事件
        initQuantityReact() {
            const vm = this;
            let col = vm.tableConfig.find(v => v.prop === 'quantities');
            col.change = (val, { row, $index }) => {
                const ret = (+row.integratedUnitPrice || 0).mul(+val).toFixed(2);
                vm.data.budgetDetailCustom[$index].totalPrice = ret;
            };
            col = vm.tableConfig.find(v => v.prop === 'integratedUnitPrice');
            col.change = (val, { row, $index }) => {
                const ret = (+row.quantities || 0).mul(+val).toFixed(2);
                vm.data.budgetDetailCustom[$index].totalPrice = ret;
            };
        },
        initTaxChangeReact() {
            const vm = this;
            // 税率项的表单配置
            const formItem = this.formList.find(item => item.prop === 'invoiceTaxRate');
            // 为select添加chagne事件
            formItem.change = val => {
                const taxMap = { 1: 0, 2: 1, 3: 3, 4: 6, 5: 9 };
                const { budgetDetailTemplate = [], budgetDetailCustom = [] } = vm.data;
                let projectBudget = 0;
                budgetDetailTemplate.forEach(v => (projectBudget += Number(v.totalPrice)));
                budgetDetailCustom.forEach(v => (projectBudget += Number(v.totalPrice)));

                vm.data.amountExcludingTax = projectBudget.toFixed(2);

                const tax = Number(taxMap[val || 1]);
                // 税率未勾选，默认按照0进行处理
                vm.data.projectBudget = (((100 + tax) / 100) * projectBudget).toFixed(2);
            };
        },
        summaryChange(sum) {
            const vm = this;
            let total = Number(sum);
            // 根据options的value拿到labe值(税率)
            const taxMap = { 1: 0, 2: 1, 3: 3, 4: 6, 5: 9 };
            const { budgetDetailTemplate = [] } = vm.data;
            budgetDetailTemplate.forEach(v => (total += Number(v.totalPrice)));
            vm.data.amountExcludingTax = total.toFixed(2);
            const tax = Number(taxMap[vm.data.invoiceTaxRate || 1]);
            // 税率未勾选，默认按照0进行处理
            vm.data.projectBudget = (((100 + tax) / 100) * total).toFixed(2);
        }
    }
};
